﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MVision;
using mtfNet;
using Public;
using static mtfNet.mtf;
using static Project.global;
using System.Data.SqlTypes;
using System.Data.Common;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SQLite;
using MyControl;

namespace Project
{
    public partial class ProjectForm : MyAlgoForm
    {
        public string m_ProjectiniPath;
        public string m_PictureFolderPath;
        public string m_PlcLogDataFolderPath;//plc log
        public string m_WorkRateFolderPath;
        public string m_CsvFolderPath;
        public ProjectDataBase ProjectDb { get; set; }

        public ProjectForm()
        {
            InitializeComponent();
            InitProjectFilePath();

            ProjectDb=new ProjectDataBase();
            InitDgv();



        }

        public ProjectForm(string str)
        {
            Console.WriteLine(str+"调用");
        }

        public void RegistCallBack()
        {
            MVision.gv.ListAutoCreateAlgoDlgFun.Add(AutoCreateAlgoDlg);
            MVision.gv.ListDispOrNewCreateDlgFun.Add(DispOrNewCreateDlg);
            MVision.gv.ListAddToToolListItemFun.Add(ProjectAddItemToToolLIst);
            var pInfoDlg = new InfoDlg();
            MVision.gv.CustomDlgMap["Info"] = pInfoDlg;

        }

        public void InitGlobalPara()
        {
            glogger = MVision.gv.glogger;
            Log.Debug(glogger, ProjectName+ " 日志设置OK!");
        }

        public static void DispOrNewCreateDlg(string strAlgo)
        {
            if (strAlgo != ProjectName) return;
            Flow.DispOrNewCreateAlgoDlg(strAlgo, ProjectClassName, ProjectAssembly);
        }

        public static void AutoCreateAlgoDlg(string strAlgo, object pflow)
        {
            if (strAlgo != ProjectName) return;
            Flow.AutoCreateAlgoDlgByLoadFlow(strAlgo, ProjectClassName, (Flow)pflow, ProjectAssembly);

        }

        public static void ProjectAddItemToToolLIst(object pTreeWidget)
        {
            //项目
            TreeNode rootNode, childNode;
            MyControl.MyTreeView obj = (MyControl.MyTreeView)pTreeWidget;
            rootNode = new TreeNode("项目");
            obj.Nodes.Add(rootNode);
            childNode = new TreeNode(ProjectName);
            rootNode.Nodes.Add(childNode);
        }

        void InitProjectFilePath()
        {
            pgv.gAppFilePath = mtf.GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File");
            pgv.gHdevFileDirPath= GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/Hdev");
            pgv.gflowDataDirPath= GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/FlowData");
            pgv.gModelFileDirPath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/Model");
            pgv.gJsonFileDirPath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/JsonFile");
            pgv.gCalibFileDirPath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/Calibrate");
            pgv.g2DCodeHandlePath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/2DCodeModel");
            pgv.gPlcCmdDirPath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/PlcCmd");
            pgv.gMetrologyHandleFileDirPath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/Metrology");
            pgv.gCameraCalibFilePath = GetmtfInstance().CreateInnerFolder(0, "DataFile/" + gProjectFolder + "/File/CameraCalibFile");
 
            m_ProjectiniPath = GetmtfInstance().CreateInnerFile(0, "DataFile/" + gProjectFolder + "/ini", gProjectFolder + ".ini");

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            m_PictureFolderPath = GetmtfInstance().CreateExteriorFolder("D:/Data/" + gProjectFolder + "Data/Picture");
            m_PlcLogDataFolderPath = GetmtfInstance().CreateExteriorFolder("D:/Data/" + gProjectFolder + "Data/PLCWarning");
            m_WorkRateFolderPath = GetmtfInstance().CreateExteriorFolder("D:/Data/" + gProjectFolder + "Data/WorkRate");
            //////////
            m_CsvFolderPath = GetmtfInstance().CreateExteriorFolder("D:/Data/" + gProjectFolder + "Data/Csv");
        }

        DataTable dgv3DataTable;
        SQLiteDataAdapter dgv3Adapter;
        void InitDgv()
        {
            //dgv3.AllowUserToAddRows = false;
            this.dgv3.CellValueChanged -= new DataGridViewCellEventHandler(this.OnCellValueChanged_dgv3);
            dgv3Adapter = ProjectDb.ConfigDb.GetAdapter("成员变量表");
            dgv3DataTable=new DataTable();
            dgv3Adapter.Fill(dgv3DataTable);
            dgv3.DataSource = dgv3DataTable;
            //BindingSource bindingSource = new BindingSource();
            //bindingSource.DataSource = dgv3DataTable;
            //dgv3.DataSource = bindingSource;
            //bindingSource.ResetBindings(true);
            this.dgv3.CellValueChanged += new DataGridViewCellEventHandler(this.OnCellValueChanged_dgv3);
            //========================================================================================






        }


        private void btn_test_Click(object sender, EventArgs e)
        {
            dgv3.Rows[2].Cells[2].Value = "8855";


        }

        private void OnCellValueChanged_dgv3(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                MyDataGridView dgv = (MyDataGridView)sender;
                dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
                this.BindingContext[dgv.DataSource].EndCurrentEdit();
                // 创建CommandBuilder
                var commandBuilder = new SQLiteCommandBuilder(dgv3Adapter);
                var res = commandBuilder.DataAdapter.Update(dgv3DataTable);
                if (res == 0)
                {
                    Log.Debug(glogger, "没有数据更新到数据库");
                }
            }
            catch (Exception ex)
            {
                MyMessageBox.Show($"同步失败：{ex.Message}");
                Log.Debug(glogger, $"同步失败：{ex.Message}");
            }
        }

        private void OnConTextMenuItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

            Log.Debug(glogger, $"右键被点击");
        }


    }
}
